<?php

namespace App\Service;

use App\AdvertiserImg;
use App\AdvertiserVideo;
use Illuminate\Support\Facades\Log;

class DunVideoService
{
    const API_URL = "https://api.dun.163yun.com";
    const INTERNAL_STRING_CHARSET = "auto";
    const SIGNATURE_METHOD = "MD5";

    const API_URL_VIDEO = "http://as.dun.163.com/v1/videosolution/submit";

    const VERSION = "v1.1";

    const API_TIMEOUT = 10;


    // 保存唯一实例的静态属性
    private static $instance = null;

    private $secretId;
    private $secretKey;
    private $businessId;

    // 构造函数私有化，防止外部实例化
    private function __construct()
    {
        // 防止外部创建对象
    }

    // 获取唯一实例的公共静态方法
    public static function getInstance()
    {
        // 如果实例不存在，创建一个新实例
        if (self::$instance === null) {
            self::$instance = new self();
        }

        // 返回唯一实例
        return self::$instance;
    }

    public function setConfig($secretId, $secretKey)
    {
        $this->secretId = $secretId;
        $this->secretKey = $secretKey;
        return $this;
    }

    function query($params){
        $params["secretId"] = $this->secretId;
//        $params["businessId"] = $this->businessId;
        $params["version"] = 'v1';
        $params["timestamp"] = time() * 1000;// time in milliseconds
        $params["nonce"] = sprintf("%d", rand()); // random int

        $params["signatureMethod"] = 'MD5';
        $params = toUtf8($params);
        $params["signature"] = gen_signature($this->secretKey, $params);
        // var_dump($params);

        $result = curl_post($params, 'http://as.dun.163.com/v1/videosolution/query/task', 10);
        if($result === FALSE){
            return array("code"=>500, "msg"=>"file_get_contents failed.");
        }else{
            return json_decode($result, true);
        }
    }


    /**
     * 反垃圾请求接口简单封装
     * $params 请求参数
     */
    public function check($params)
    {
        $params["secretId"] = $this->secretId;
        $params["version"] = self::VERSION;
        $params["timestamp"] = time() * 1000;// time in milliseconds
        $params["nonce"] = sprintf("%d", rand()); // random int
        $params["signatureMethod"] = self::SIGNATURE_METHOD;

        $params = toUtf8($params);
        $params["signature"] = gen_signature($this->secretKey, $params);

        $result = curl_post($params, self::API_URL_VIDEO, self::API_TIMEOUT);
        // var_dump($result);
        if ($result === FALSE) {
            return ["code" => 500, "msg" => "file_get_contents failed."];
        } else {
            return json_decode($result, true);
        }
    }

    // 简单测试
    public function main(array $params)
    {
        if (empty($params)){
            return false;
        }
        if (empty($params['url'])){
            return false;
        }
        $ret = $this->check($params);
        if (isset($ret["code"]) && $ret["code"] == 200) {
            $result = $ret["result"];
            $dataId = $result["dataId"];
            $taskId = $result["taskId"];

            $advertiserImage = AdvertiserVideo::query()->where('video_id', $dataId)->where('status', 0)->first();
            if (!empty($advertiserImage)){
                $advertiserImage->task_id = $taskId;
                $advertiserImage->status = 1;
                $advertiserImage->save();
            }

        } else {
            Log::info("易盾视频检查异常", [
                'param' => $params,
                'result' => $ret
            ]);
        }
    }
}
